<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_gedung extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.kd_gedung';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('*, a.nama_gedung, b.Nama AS nama_kampus', false);
			$this->db->from('ref_gedung a');
			$this->db->join('ref_kampus b', 'b.kd_kampus = a.kd_kampus','left');
			
			$this->db->where('a.status', "A");
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('*, a.nama_gedung, b.Nama AS nama_kampus', false);
		$this->db->from('ref_gedung a');
		$this->db->join('ref_kampus b', 'b.kd_kampus = a.kd_kampus','left');
		$this->db->where('a.kd_gedung', $kode);
		
		return $this->db->get();
	}
	
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('kd_gedung',$kode);
		
		$this->db->select('*');
		$this->db->from('ref_gedung');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_kampus', $data['kd_kampus']);
		$this->db->set('kd_gedung', $data['kd_gedung']);
		$this->db->set('nama_gedung', $data['nama_gedung']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
		
		$result = $this->db->insert('ref_gedung');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_kampus', $data['kd_kampus']);
		$this->db->set('kd_gedung', $data['kd_gedung']);
		$this->db->set('nama_gedung', $data['nama_gedung']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('kd_gedung', $data['kd_gedung']);
		$result = $this->db->update('ref_gedung');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		//query delet data  dengan ganti status db            
		$this->db->flush_cache();
		
		$this->db->set('status', "T");
	   
		$this->db->set('MUID', $this->session->userdata('user_id'));
		$this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('kd_gedung', $kode);
		$result = $this->db->update('ref_gedung');
	   
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}
	
	/*function get_pdf(){
		# get data
		$this->db->flush_cache();
		$this->db->select('*, a.nama_gedung, b.Nama AS nama_kampus', false);
		$this->db->from('ref_gedung a');
		$this->db->join('ref_kampus b', 'b.kd_kampus = a.kd_kampus','left');
		
		$this->db->where('a.status', "A");

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Kampus', 'width'=>100, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Kode gedung', 'width'=>45, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama gedung', 'width'=>120, 'align'=>'L', 'val_align'=>'L'));
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->nama_kampus,
				$row->kd_gedung,				
				$row->nama_gedung
			));
			$i++;
		}
		
		return $pdfdata;
		
	}*/
	
	//PDF
	function get_pdf(){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		$this->db->flush_cache();
		$this->db->select('*, a.nama_gedung, b.Nama AS nama_kampus', false);
		$this->db->from('ref_gedung a');
		$this->db->join('ref_kampus b', 'b.kd_kampus = a.kd_kampus','left');
			
		$this->db->where('a.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
	public function importData($data){
		//query insert data
		$this->db->flush_cache();
		
		$this->db->set('kd_kampus',				$data['kd_kampus']);
		$this->db->set('kd_gedung',				$data['kd_gedung']);
		$this->db->set('nama_gedung',			$data['nama_gedung']);
		$this->db->set('status',				$data['status']);
		
		$result = $this->db->insert('ref_gedung');
		
		$errNo   = $this->db->_error_number();
	    $errMess = $this->db->_error_message();
		$error = $errMess;
		//var_dump($errMess);die;
	    log_message("error", "Problem import Inserting to : ".$errMess." (".$errNo.")"); 
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
}

?>